//
//  SQLiteManager.h
//  Rich
//
//  Created by Kenny Chen on 12/12/7.
//  Copyright (c) 2012年 Kenny Chen. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <sqlite3.h>

@class SQLMResultSet;

@interface SQLiteManager : NSObject
{
    sqlite3 * database;
}

@property (nonatomic, retain) NSMutableDictionary * dbStruct;
@property (nonatomic, retain) NSString *dbPath;
@property (assign) BOOL isOpen;

- (int)open:(NSString *)filename;
- (void)close;
- (int)execute:(NSString *)sql;
- (NSMutableArray *)listTables;
- (int)createTable:(NSString *)table columnDefinitions:(NSString *)columnDef;
- (int64_t)insertColumns:(NSString *)columns withValues:(NSString *)values intoTable:(NSString *)table;
- (int)updateTable:(NSString *)table setColumns:(NSString *)syntax where:(NSString *)conditions;
- (int)deleteFromTable:(NSString *)table where:(NSString *)conditions;
- (SQLMResultSet *)selectColumns:(NSString *)columns fromTable:(NSString *)table;
- (SQLMResultSet *)selectColumns:(NSString *)columns fromTable:(NSString *)table limit:(NSString *)limit;
- (SQLMResultSet *)selectColumns:(NSString *)columns fromTable:(NSString *)table orderBy:(NSString *)orderByColumn descending:(BOOL)descending;
- (SQLMResultSet *)selectColumns:(NSString *)columns fromTable:(NSString *)table orderBy:(NSString *)orderByColumn descending:(BOOL)descending limit:(NSString *)limit;
- (SQLMResultSet *)selectColumns:(NSString *)columns fromTable:(NSString *)table where:(NSString *)conditions;
- (SQLMResultSet *)selectColumns:(NSString *)columns fromTable:(NSString *)table where:(NSString *)conditions orderBy:(NSString *)orderByColumn descending:(BOOL)descending;
- (SQLMResultSet *)selectColumns:(NSString *)columns fromTable:(NSString *)table where:(NSString *)conditions limit:(NSString *)limit;
- (SQLMResultSet *)selectColumns:(NSString *)columns fromTable:(NSString *)table where:(NSString *)conditions orderBy:(NSString *)orderByColumn descending:(BOOL)descending limit:(NSString *)limit;

@end

#pragma mark -

@interface SQLMResultSet : NSObject
{
    NSMutableDictionary *_columnNameToIndexMap;
}

@property (nonatomic, assign) sqlite3_stmt **statement;
@property (assign) NSUInteger dataCount;

- (void)close;
- (BOOL)next;

- (NSMutableDictionary *)columnNameToIndexMap;
- (int)columnIndexForName:(NSString *)columnName;

- (int)intForColumn:(NSString *)columnName;
- (int)intForColumnIndex:(int)columnIdx;

- (long)longForColumn:(NSString *)columnName;
- (long)longForColumnIndex:(int)columnIdx;

- (long long int)longLongIntForColumn:(NSString *)columnName;
- (long long int)longLongIntForColumnIndex:(int)columnIdx;

- (unsigned long long int)unsignedLongLongIntForColumn:(NSString *)columnName;
- (unsigned long long int)unsignedLongLongIntForColumnIndex:(int)columnIdx;

- (BOOL)boolForColumn:(NSString *)columnName;
- (BOOL)boolForColumnIndex:(int)columnIdx;

- (double)doubleForColumn:(NSString *)columnName;
- (double)doubleForColumnIndex:(int)columnIdx;

- (NSString *)stringForColumn:(NSString *)columnName;
- (NSString *)stringForColumnIndex:(int)columnIdx;

- (NSDate *)dateForColumn:(NSString *)columnName;
- (NSDate *)dateForColumnIndex:(int)columnIdx;

- (NSData *)dataForColumn:(NSString *)columnName;
- (NSData *)dataForColumnIndex:(int)columnIdx;

- (NSData *)dataNoCopyForColumn:(NSString *)columnName;
- (NSData *)dataNoCopyForColumnIndex:(int)columnIdx;

@end